from utils import *
import os
import information
import json
import friends

MODULENAME = 'apps'

INFO_ID_APPS_LIST = information.INFO_ID_APPS + "/list"

#list of plugins available
pluginsList = []

#load plugins
for file in os.listdir(MODULENAME):
    filePath = os.path.join(MODULENAME,file)
    if os.path.isdir(filePath):
        importObject = __import__(file, globals(), locals(), [], -1)
        importObject.setPath(filePath)
        pluginsList.append(importObject)

#compare and update plugin list with what is registered in base
titleList = []
for plugins in pluginsList:
    titleList.append(plugins.getTitle())
appsInfo = information.getPersonalInformation(INFO_ID_APPS_LIST)
if appsInfo:
    listApps = json.loads(appsInfo.getData())
    if listApps != titleList:
        #update list apps
        information.setPersonalInformation(INFO_ID_APPS_LIST,friends.FRIENDSGROUP_ALL,json.dumps(titleList))
else:
    #first set up of apps list
    information.setPersonalInformation(INFO_ID_APPS_LIST,friends.FRIENDSGROUP_ALL,json.dumps(titleList))
    
#launch get of app list of peers
for friend in friends.getFriendsPool().getAllFriends():
    information.getInformation(INFO_ID_APPS_LIST,friend)
    